package com.cdut.logistics.intercepter.system;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.cdut.logistics.model.dao.datacentre.User;
public class LoginIntercepter implements Interceptor {
	User userdao=new User();
	public void intercept(Invocation ai) {
		Controller controller=ai.getController();
		HttpSession sen = ai.getController().getSession();
		Map m = new HashMap();
		String uri=controller.getRequest().getRequestURI();
		
		String access_token=(String) sen.getAttribute("access_token");
		boolean result = false;
		if(uri.equals("/ulms_project/user1/login")||uri.equals("/ulms_project/user1/renderLogin")){
			ai.invoke();
			System.out.println("登陆操作，放过");
		}else{
		if(access_token!=null){
		try {
			result=userdao.validateIfLogin(access_token);
		} catch (Exception e) {
			System.out.println("200 数据库系统错误");
			m.put("result",result);
//			ai.getController().renderJson(m);
			ai.getController().redirect("/user1/renderLogin");
		}
		if(result){
			ai.invoke();
			System.out.println("登陆验证通过");
			
		}
		else{
			System.out.println("登陆验证失败,被拦截");
			m.put("result",result);
			//ai.getController().renderJson(m);
			ai.getController().redirect("/user1/renderLogin");
		}
		}
		else{
			System.out.println("登陆验证失败,被拦截");
			m.put("result",result);
//			ai.getController().renderJson(m);
			ai.getController().redirect("/user1/renderLogin");

		}
		}
	}
}
